﻿Imports System.Data
Imports System.Data.Odbc
Imports System.Text
Imports System.Collections.Generic
Imports WSEntidades
Imports Utilerias
Imports WSFactory
Imports MySql.Data.MySqlClient
Public Class CDonatariasDAL
    Private cadConex As String = ""

    Public Sub New(ByVal pCadConex As String)
        Me.cadConex = pCadConex
    End Sub

    Public Function actualizaDonatariasConfig(ByVal pDonatarias As CDonatariasBE) As Boolean
        Dim boolActualizado As Boolean = False
        Try
            Using myConn As MySqlConnection = New MySqlConnection(Me.cadConex)
                Using myComm As MySqlCommand = myConn.CreateCommand()
                    myComm.CommandText = "UPDATE configcomplementos " & _
                                            "SET Valor = @Valor " & _
                                            "WHERE CveComp = 'donat' AND CveConfig = @cveConfig; "
                    myComm.CommandType = CommandType.Text
                    myComm.Parameters.Clear()
                    myComm.Parameters.AddWithValue("@Valor", pDonatarias.Donataria.version)
                    myComm.Parameters.AddWithValue("@cveConfig", "Version")
                    myConn.Open()
                    If (myComm.ExecuteNonQuery() = 1) Then
                        myComm.Parameters.Clear()
                        myComm.Parameters.AddWithValue("@Valor", pDonatarias.Donataria.noAutorizacion)
                        myComm.Parameters.AddWithValue("@cveConfig", "NumAut")
                        If (myComm.ExecuteNonQuery() = 1) Then
                            myComm.Parameters.Clear()
                            myComm.Parameters.AddWithValue("@Valor", pDonatarias.Donataria.fechaAutorizacion.ToString("dd-MM-yyyy"))
                            myComm.Parameters.AddWithValue("@cveConfig", "FechaAut")
                            If (myComm.ExecuteNonQuery() = 1) Then
                                myComm.Parameters.Clear()
                                myComm.Parameters.AddWithValue("@Valor", pDonatarias.Donataria.leyenda)
                                myComm.Parameters.AddWithValue("@cveConfig", "Leyenda")
                                If (myComm.ExecuteNonQuery() = 1) Then
                                    boolActualizado = True
                                End If
                            End If
                        End If
                    End If
                End Using
            End Using

            Return boolActualizado
        Catch ex As Exception
            Throw
        Finally
            boolActualizado = Nothing
        End Try
    End Function

    Public Function actualizaDonatariasComp(ByVal pDonatarias As CDonatariasBE) As Boolean
        Try
            Using myConn As MySqlConnection = New MySqlConnection(Me.cadConex)
                Using myComm As MySqlCommand = myConn.CreateCommand()
                    myComm.CommandText = "UPDATE complementos " & _
                                            "SET Status = @Status, RegimenFiscal = @RegimenFiscal " & _
                                            "WHERE CveComp = 'donat'; "
                    myComm.CommandType = CommandType.Text
                    myComm.Parameters.Clear()
                    Select Case pDonatarias.Status
                        Case CDonatariasBE.EstatusDonat.Activa
                            myComm.Parameters.AddWithValue("@Status", "S")
                        Case CDonatariasBE.EstatusDonat.Inactiva
                            myComm.Parameters.AddWithValue("@Status", "N")
                    End Select
                    myComm.Parameters.AddWithValue("@RegimenFiscal", pDonatarias.RegimenFiscal)
                    myConn.Open()
                    If (myComm.ExecuteNonQuery() = 1) Then
                        Return True
                    Else
                        Return False
                    End If
                End Using
            End Using
        Catch ex As Exception
            Throw
        End Try
    End Function

    Public Function getDonatarias() As CDonatariasBE
        Dim tmpDonatarias As CDonatariasBE
        Try
            Using myConn As MySqlConnection = New MySqlConnection(Me.cadConex)
                Using myComm As MySqlCommand = myConn.CreateCommand()
                    myComm.CommandText = "SELECT complementos.Status, " & _
                                                "complementos.RegimenFiscal, " & _
                                                "configcomplementos.CveComp, " & _
                                                "configcomplementos.CveConfig, " & _
                                                "configcomplementos.Valor " & _
                                            "FROM complementos " & _
                                                "INNER JOIN configcomplementos " & _
                                                        "ON (complementos.CveComp = configcomplementos.CveComp) " & _
                                            "WHERE configcomplementos.CveComp = 'donat' ;"
                    myComm.CommandType = CommandType.Text
                    myConn.Open()
                    Using myDR As MySqlDataReader = myComm.ExecuteReader()
                        If (myDR.HasRows()) Then
                            tmpDonatarias = CDonatariasFact.Build(myDR)
                        Else
                            tmpDonatarias = Nothing
                        End If
                    End Using
                End Using
            End Using
            Return tmpDonatarias

        Catch ex As Exception
            Throw
        Finally
            tmpDonatarias = Nothing
        End Try
    End Function
End Class
